package tut.designParttern.design.flyweight.source;

import java.util.ArrayList;

public class FlyWeightExample {
	public static void main(String[] args) {
		GraphicCharFactory cf = new GraphicCharFactory();

		// Compose the text by storing the characters as objects.
		ArrayList<GraphicChar> text = new ArrayList<GraphicChar>();
		text.add(cf.get('H', "Arial")); // 'H' and "Arial" are called intrinsic
										// information
		text.add(cf.get('e', "Arial")); // because it is stored in the object
										// itself.
		text.add(cf.get('l', "Arial"));
		text.add(cf.get('l', "Arial"));
		text.add(cf.get('o', "Times"));

		// See how the Flyweight approach is beginning to save space:
		System.out.printf(
				"CharFactory created only %d objects for %d characters.\n",
				cf.getNum(), text.size());

		int x = 0, y = 0;
		for (GraphicChar c : text) { // Passing position as extrinsic
										// information to the objects,
			c.printAtPosition(x++, y); // as a top-left 'A' is not different to
										// a top-right one.
		}
	}
}
